package com.wenx.unit2.chapter4;

import java.util.*;

import static java.util.Comparator.*;
import static java.util.stream.Collectors.*;

public class Chapter4 {

    public static void main(String[] args) {
        List<Dish> menu = Arrays.asList(new Dish("pork", false, 800, Dish.Type.MEAT),
                new Dish("beef", false, 700, Dish.Type.MEAT),
                new Dish("chicken", false, 400, Dish.Type.MEAT),
                new Dish("french fries", true, 530, Dish.Type.OTHER),
                new Dish("rice", true, 350, Dish.Type.OTHER),
                new Dish("season fruit", true, 120, Dish.Type.OTHER),
                new Dish("pizza", true, 550, Dish.Type.OTHER),
                new Dish("prawns", false, 300, Dish.Type.FISH),
                new Dish("salmon", false, 450, Dish.Type.FISH));

         List<String> menuNameList = menu.stream().filter(dish -> dish.getCalories() >300)
                .limit(3)
                .map(Dish::getName)
                .collect(toList());

        Map<Dish.Type, List<Dish>> typeListMap = menu.stream().collect(groupingBy(Dish::getType));

        Optional<Dish> dishOptional = menu.stream().max(comparingInt(Dish::getCalories));

        IntSummaryStatistics intSummaryStatistics = menu.stream().collect(summarizingInt(Dish::getCalories));

        Integer collect = menu.stream().collect(summingInt(Dish::getCalories));

        String collect1 = menu.stream().map(Dish::getName).collect(joining(","));
        System.out.println("collect1 = " + collect1);

        Integer collect2 = menu.stream().collect(reducing(0, Dish::getCalories, (i, j) -> i + j));

        Integer collect3 = menu.stream().mapToInt(Dish::getCalories).reduce(0, (i, j) -> i + j);


        List<Dish> collect4 = menu.stream()
                .sorted(comparing(dish -> dish.getName().length(),reverseOrder())).collect(toList());
        collect4.forEach(dish -> System.out.println(dish.getName()));

    }


//    public static List<Dish> filterDish(List<Dish> dishes, Predicate<Dish> dishPredicate){
//        List<Dish> dishList = new ArrayList<>();
//
//    }

}
